<?php
error_reporting( E_ALL );
require_once('simpletest/unit_tester.php');
require_once('simpletest/reporter.php');
require_once('../lib/include.php');

class TestRegistrySF extends UnitTestCase
{
	var $_registry;
	
	function TestRegistrySF( $ob )
	{
		$this->_registry = $ob;
	}
	function testSetters ()
	{
		$this->_testSettersForField( 'firstname' );
		$this->_testSettersForField( 'age' );
		$this->_testSettersForField( 'firstname' );
		$this->_testSettersForField( 'surname' );
	}
	function _testSettersForField ( $key )
	{
		$o = $this->getOb();
		
		$this->assertError( $o->get( $key ) );
		$this->assertFalse( $o->exists( $key ));
		foreach ( $this->_testValues() as $value )
		{
			$this->_testSetter( $o, $key, $value );	
		}
		$this->assertTrue( $o->exists( $key ));
		$o->destroy( $key );
		$this->assertFalse( $o->exists( $key ));
	}
	function _testValues()
	{
		return array 
		(
			0
			, 1
			, 122165126
			, '1'
			, '5'
			, true
			, false
			, array ( 1,2, 'daffy')
			, new stdClass()
		);
	}
	function _testSetter ( $object, $key, $value )
	{
		$inserted = $object->set( $key, $value );
		$this->assertTrue( $inserted, "key '$key' failed setting to value '$value'" );
		$getted = $object->get( $key );
		$this->assertIdentical( $value, $getted );
	}
	function getOb ()
	{
		return $this->copy( $this->_registry );
	}
	function copy($object) {
		if (version_compare(phpversion(), '5') >= 0) {
			eval('$copy = clone $object;');
			return $copy;
		}
			return $object;
    	}
}

Class TestRecordSF extends TestRegistrySF
{
	function testUnacceptableSetters ()
	{
		$this->_testSettersForNonexistingField( 'aaaaa' );
	}
	function _testSettersForNonexistingField ( $key )
	{
		$o = $this->getOb();
		
		$this->assertError( $o->get( $key ) );
		$this->assertFalse( $o->exists( $key ));
		foreach ( $this->_testValues() as $value )
		{
			$this->_testUnacceptableSetter( $o, $key, $value );	
			
		}
		$this->assertFalse( $o->exists( $key ));
	}
	function _testUnacceptableSetter ( $object, $key, $value )
	{
		$inserted = $object->set( $key, $value );
		$this->assertFalse( $inserted, "key '$key' should fail setting anythin. We succeeded with value '$value'" );
		$this->assertError( $object->get( $key ) );
		$this->assertFalse( $object->exists( $key ));
	}
}

$test = &new TestRegistrySF( new DictionarySF() );
$test->run(new HtmlReporter());

$Record = new RecordSF();
$Record->setFields( array ( 'ID', 'firstname', 'surname', 'age' ) );

$test = &new TestRecordSF( $Record );
$test->run(new HtmlReporter());

?>